<template>
  <div class="list-table">
    <el-form ref="form" label-width="120px" class="dataForm" style="left: 20px;right:5px;">
      <!-- 基金收益列表 -->
      <div class="area-header" style="margin-top: 10px;">
        <span class="area-header-title">{{ $t('基金收益列表') }}</span>
      </div>
      <el-table ref="multipleTable" v-loading="loading" :data="fundIncomeList" :row-key="getRowKey" tooltip-effect="dark" border @selection-change="getCheckedData">
        <el-table-column
          v-model="checkedChange"
          reserve-selection
          type="selection"
          width="55"
        />
        <el-table-column v-if="dataSource === 'deal'" :label="$t('回款来源')" align="center" prop="dealName">
          <template slot-scope="scope">
            <span>{{ scope.row.dealName ? scope.row.dealName : typeFeeFormat(scope.row) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('回款时间')" align="center" prop="distributionDate">
          <template slot-scope="scope">
            <span>{{ parseTime(scope.row.distributionDate) }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('来源类型')" align="center" prop="dealType" :formatter="dealTypeFormat" />
        <el-table-column :label="$t('本金') + $t('金额单位')" align="center" prop="amountPrincipal">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.amountPrincipal, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('收益') + $t('金额单位')" align="center" prop="amountIncome">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.amountIncome, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('分红') + $t('金额单位')" align="center" prop="amountBonus">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.amountBonus, 'w') }}</span>
          </template>
        </el-table-column>
        <el-table-column :label="$t('其他') + $t('金额单位')" align="center" prop="amountOthers">
          <template slot-scope="scope">
            <span>{{ moneyFormat(scope.row.amountOthers, 'w') }}</span>
          </template>
        </el-table-column>
      </el-table>
    </el-form>
    <div slot="footer" class="dialog-footer" style="text-align: right;">
      <!--保存，保存并启动流程，取消 三个按钮-->
      <el-button @click="cancel">{{ $t("取消") }}</el-button>
      <el-button type="primary" @click="confirmChecked">{{ $t('确认') }}</el-button>
    </div>
  </div>
</template>

<script>
  import { listFundIncome } from '@/api/fund/income/fundIncome'

export default {
  data() {
    return {
      // 变更历史遮罩层
      loading: true,
      // 对比遮罩层
      loadingVS: false,
      // 查询参数
      queryParams: {
        fundId: this.$route.query.id,
        dataSource: ''
      },
      // 基金收益表格数据
      fundIncomeList: [],
      parentList: [],
      // 区分是需要项目回款还是手动添加的收益信息
      dataSource: 'deal',
      // 选中的ID
      checkedChange: [],
      // 项目类型字典
      dealTypeOptions: [],
      // 选中的对象
      checkedObj: [],
      // 回显的对象
      checkedList: [],
      // 费用类型
      typeFeeOptions: []
    }
  },
  created() {
    this.getDicts('income_source_type').then(response => {
      this.dealTypeOptions = response.data
    })
    this.getDicts('fund_income_type_fee').then(response => {
      response.data.push({
        dictLabel: '项目收益',
        dictValue: 'XMSY'
      })
      this.typeFeeOptions = response.data
    })
    this.getList()
  },
  methods: {
    /** 查询股东信息列表 */
    getList() {
      this.loading = true
      listFundIncome(this.queryParams).then(response => {
        this.fundIncomeList = response.rows
        this.total = response.total
        this.loading = false
      })
    },
    typeFeeFormat(row) {
      return this.selectDictLabel(this.typeFeeOptions, row.typeFee)
    },
    // 回显选中的数据
  // getIncomeList(list) {
  //     console.log(list)
  //     listFundIncome(this.queryParams).then(response => {
  //       this.fundIncomeList = response.rows
  //       this.checkedList = []
  //       this.checkedChange = []
  //       this.checkedObj = []
  //       console.log(list)
  //       this.$nextTick(() => {
  //         list.forEach((item) => {
  //           this.$refs.multipleTable.toggleRowSelection(
  //             this.fundIncomeList.find((dd) => {
  //               return item.incomeId === dd.id || item.id === dd.id
  //             }),
  //             true
  //           )
  //         })
  //       })
  //       this.checkedList = list
  //       this.total = response.total
  //       this.loading = false
  //     })
  //   },
    getIncomeList(list) {
      this.$refs.multipleTable.clearSelection()
      this.parentList = []
      this.checkedObj = []
      listFundIncome(this.queryParams).then(response => {
        this.fundIncomeList = response.rows
        this.checkedList = []
        this.parentList = list
        this.checkedChange = []
        console.log(list)
        this.$nextTick(() => {
          list && list.forEach(item => {
            let idx = 0
            this.fundIncomeList.forEach(dd => {
              if (item.incomeId === dd.id) {
                this.checkedList.push(item)
                this.$refs.multipleTable.toggleRowSelection(this.fundIncomeList[idx], true)
              }
              idx++
            })
          })
        })
        this.total = response.total
        this.loading = false
      })
    },
    // 项目类型
    dealTypeFormat(row) {
      return this.selectDictLabel(this.dealTypeOptions, row.dealType)
    },
    // 获取rowKey
    getRowKey(row) {
      return row.id
    },
    // 根据选中的ID，展示对比信息
    getCheckedData(val) {
      this.checkedObj = []
      if (val !== null && val !== undefined) {
        val.forEach(item => {
          if (item !== undefined) {
            var row = {
              id: item.id,
              // 回款项目
              dealName: item.dealName,
              // 项目类型
              dealType: item.dealType,
              // 费用类型
              typeFee: item.typeFee,
              // 回款时间
              distributionDate: item.distributionDate,
              // 本金
              amountPrincipal: item.amountPrincipal,
              // 收益
              amountIncome: item.amountIncome,
              // 分红
              amountBonus: item.amountBonus,
              // 其他
              amountOthers: item.amountOthers
            }
            this.checkedObj.push({ fundIncome: row })
          }
        })
      }
    },
    // getCheckedData(val) {
    //   this.checkedObj = []
    //   debugger
    //   if (val !== null && val !== undefined) {
    //     val.forEach(item => {
    //       debugger
    //       var row = {
    //         id: item.id,
    //         // 回款项目
    //         dealName: item.dealName,
    //         // 回款时间
    //         distributionDate: item.distributionDate,
    //         // 本金
    //         amountPrincipal: item.amountPrincipal,
    //         // 收益
    //         amountIncome: item.amountIncome,
    //         // 分红
    //         amountBonus: item.amountBonus,
    //         // 其他
    //         amountOthers: item.amountOthers
    //       }
    //       this.checkedObj.push({ fundIncome: row })
    //     })
    //   }
    // },

    // 点击确认，将选中的收益数据回显到基金分配页面
    confirmChecked() {
      if (this.checkedObj === [] || this.checkedObj.length === 0) {
        this.msgError(this.$t('请选择收益！'))
        return
      }
      var fundIncomeList = []
      this.checkedObj.forEach(item => {
        if (this.dataSource === 'deal') {
          this.checkedList.forEach(deal => {
            if (deal.fundIncome.id !== item.fundIncome.id) {
              fundIncomeList.push(item)
            } else {
              fundIncomeList.push(deal)
            }
          })
        } else {
          this.checkedList.forEach(other => {
            if (other.incomeId !== item.id) {
              this.checkedList.push(item)
            }
          })
        }
      })
      var idListStr = ''
      this.parentList.forEach(item => {
        idListStr = idListStr + item.fundIncome.id + ','
      })
      if (fundIncomeList === []) {
        if (this.dataSource === 'deal') {
          this.$parent.$parent.form.dealIncomeList = fundIncomeList
        } else {
          this.$parent.$parent.form.otherIncomeList = fundIncomeList
        }
      } else {
        this.checkedObj.forEach(item => {
          if (this.dataSource === 'deal') {
            if (idListStr.lastIndexOf(item.fundIncome.id) === -1) {
              this.$parent.$parent.form.dealIncomeList.push(item)
            }
          } else {
            this.$parent.$parent.form.otherIncomeList.push(item)
          }
        })
      }
      this.$parent.$parent.getAllocationSum()
      this.$parent.$parent.formOpen = false
    },
    // confirmChecked() {
    //   if (this.checkedObj === [] || this.checkedObj.length === 0) {
    //     this.msgError(this.$t('请选择收益！'))
    //     return
    //   }
    //
    //   this.checkedObj.forEach(item => {
    //     debugger
    //     if (this.dataSource === 'deal') {
    //       this.checkedList.forEach(deal => {
    //         debugger
    //         if (deal.incomeId !== item.fundIncome.id) {
    //           if (deal.fundIncome.id !== item.fundIncome.id) {
    //             this.checkedList.push(item)
    //           }
    //         }
    //       })
    //     } else {
    //       this.checkedList.forEach(other => {
    //         if (other.incomeId !== item.id) {
    //           this.checkedList.push(item)
    //         }
    //       })
    //     }
    //   })
    //     if (this.dataSource === 'deal') {
    //       this.$parent.$parent.form.dealIncomeList = this.checkedList
    //     } else {
    //       this.$parent.$parent.form.otherIncomeList = this.checkedList
    //     }
    //   this.$parent.$parent.formOpen = false
    // },
    // 取消按钮
    cancel() {
      this.$parent.$parent.formOpen = false
    }
  }
}
</script>
